﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoLoginAndRun
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public MainForm(bool elevationNeeded) : this()
        {
            if (elevationNeeded == false)
            {
                applyButton.Image = null;
            }
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            if (loginCheckBox.Checked)
            {
                if (userTextBox.Text.Trim() == "")
                {
                    MessageBox.Show(this, "Please specify the login user name.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (startCheckBox.Checked)
            {
                if (pathTextBox.Text.Trim() == "")
                {
                    MessageBox.Show(this, "Please specify the application path to be executed.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else if (System.IO.File.Exists(pathTextBox.Text) == false)
                {
                    MessageBox.Show(this, "The selected path is not a valid file. Please specify a new path.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else if (loginCheckBox.Checked && string.Compare(userTextBox.Text, Environment.UserName, true) != 0)
                {
                    DialogResult r = MessageBox.Show(this,
                        "The application auto start will be performed on current user \"" + Environment.UserName + "\" while auto login will be on a different user. Are you sure you want to continue?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (r == System.Windows.Forms.DialogResult.Cancel)
                        return;
                }
            }

            Properties.Settings.Default.Save();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = pathTextBox.Text;
            if (openFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                pathTextBox.Text = openFileDialog.FileName;
            }
        }

        private void helpButton_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://winautologin.codeplex.com/documentation");
        }
    }
}
